package org.kl.bf.service.basic;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.kl.bf.entity.basic.DocumentContent;
import org.kl.bf.entity.basic.HelpDocument;
import org.kl.bf.repository.basic.HelpDocumentDao;
import org.kl.bf.service.ServiceException;
import org.kl.bf.utils.CRUDUtils;
import org.kl.bf.utils.KLDynamicSpecifications;
import org.kl.bf.utils.KLSearchFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.utils.Collections3;

//Spring Bean的标识.
@Component
//默认将类中的所有public函数纳入事务管理.
@Transactional
public class HelpDocumentService {
	
	private HelpDocumentDao helpDocumentDao;
	
	@Autowired
	private DocumentContentService documentContentService;

	@Autowired
	public void setHelpDocumentDao(HelpDocumentDao helpDocumentDao) {
		this.helpDocumentDao = helpDocumentDao;
	}

	public HelpDocument getHelpDocument(String guid) {
		return helpDocumentDao.find(HelpDocument.class, guid);
		//return helpDocumentDao.findOne(guid);
	}

	public void saveHelpDocument(HelpDocument entity) {
		helpDocumentDao.save(entity);
	}

	public void deleteHelpDocument(String guid) {
		helpDocumentDao.delete(guid);
	}

	public int childrenCount(final String pcode) {
		Specification<HelpDocument> spec = new Specification<HelpDocument>() {
			public Predicate toPredicate(Root<HelpDocument> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				Predicate p2 = cb.equal(root.get("helpParentId").as(String.class), pcode);
				query.where(p2);
				return query.getRestriction();
			}
		};
		return (int) helpDocumentDao.count(spec);
	}

	public int batchDeleteHelpDocument(String ids) {
		String[] idArray = ids.split(",");
		int deletedCount = 0;
		for (String id : idArray) {
			if (StringUtils.isBlank(id)) {
				continue;
			}
			HelpDocument helpDocument = helpDocumentDao.findOne(id);
			int count = childrenCount(helpDocument.getId());
			if (count != 0) {
				throw new ServiceException(helpDocument.getHelpDocTitle() + "不是叶子节点，不能删除");
			}
			deletedCount++;
		}
		for (String id : idArray) {
			if (StringUtils.isBlank(id)) {
				continue;
			}
			DocumentContent dc = documentContentService.getDCByHelpDocId(id);
			if(dc!=null){
				documentContentService.deleteDocumentContent(dc.getId());
			}
			helpDocumentDao.delete(id);
		}
		return deletedCount;
	}

	public List<HelpDocument> findById(String id) {
		return helpDocumentDao.findById(id);
	}
	
	/*public List<HelpDocument> findByOrgNo(String orgNo) {
		return helpDocumentDao.findByOrgNo(orgNo);
	}*/

	public List<HelpDocument> findByHelpParentId(String helpParentId) {
		return helpDocumentDao.findByHelpParentId(helpParentId);
	}

	public List<HelpDocument> getAllHelpDocument() {
		return helpDocumentDao.findAll(new Sort(Direction.ASC, "helpDocTitle"));
		//return helpDocumentDao.findAll();
	}
//获取根节点
	public List<HelpDocument> findRoot() {
		return helpDocumentDao.findRoot();
	}
//获取子节点
	public List<HelpDocument> findByHelpDocumentsHelpParentIdIn(Collection<String> helpParentId) {
		if (Collections3.isEmpty(helpParentId)) {
			return null;
		}
		return helpDocumentDao.findByHelpParentIdIn(helpParentId);
	}

	public Page<HelpDocument> getPbs(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortType, String orderType) {
		PageRequest pageRequest = CRUDUtils.buildPageRequest(pageNumber, pageSize, sortType, orderType);
		Specification<HelpDocument> spec = buildSpecification(searchParams);

		return helpDocumentDao.findAll(spec, pageRequest);
	}

	/**
	 * 创建动态查询条件组合.
	 */
	private Specification<HelpDocument> buildSpecification(Map<String, Object> searchParams) {
		Map<String, KLSearchFilter> filters = KLSearchFilter.parse(searchParams);
		Specification<HelpDocument> spec = KLDynamicSpecifications.byKLSearchFilter(filters.values(), HelpDocument.class);
		return spec;
	}

}
